package com.finance.rule.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.finance.base.constant.BaseConstants;
import com.finance.base.exception.BaseException;
import com.finance.framework.cache.CacheSource;
import com.finance.framework.cache.RedisConstnats;
import com.finance.framework.lang.Page;
import com.finance.framework.lang.PageResultBO;
import com.finance.rule.bean.pojo.MixSectionRule;
import com.finance.rule.dao.MixSectionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by CBWL on 2017/3/17.
 */
@Service
public class MixSectionService implements IMixSectionService {

    @Autowired
    private MixSectionMapper mixSectionMapper;


    private ObjectMapper objectMapper = new ObjectMapper();


    @Override
    public PageResultBO<MixSectionRule> listByPage(Page page) {
        PageResultBO<MixSectionRule> pageResultBO = new PageResultBO<MixSectionRule>();
        List<MixSectionRule>list = mixSectionMapper.listByPage(page);
        pageResultBO.setPageNo(page.getPageNo());
        pageResultBO.setPageSize(page.getPageSize());
        pageResultBO.setTotalRecord(page.getTotalRecord());
        pageResultBO.setDataList(list);
        return pageResultBO;
    }

    @Override
    @Cacheable(value = "rule",cacheResolver = "ruleCacheResolver", key = "'mixrule_'+#id",condition = "#id gt 0")
    public MixSectionRule getById(Long id){
        MixSectionRule mixSectionRule = this.mixSectionMapper.getById(id);
        return mixSectionRule;
    }

    @Override
    @CacheEvict(value = "rule",cacheResolver = "ruleCacheResolver",key = "'mixrule_'+#id",condition = "#result == true")
    public boolean add(MixSectionRule rule){
        return mixSectionMapper.add(rule)==1;
    }

    @Override
    @CachePut(value = "rule",cacheResolver = "ruleCacheResolver", key = "'mixrule_'+ '#rule.id' ",condition = "#result == true")
    public boolean update(MixSectionRule rule){
        MixSectionRule oldRule = mixSectionMapper.getById(rule.getId());
        if(oldRule == null){
            throw new BaseException(BaseConstants.ERR_NON_EXISTENT);
        }
        oldRule.setSnapshot(null);
        try {
            rule.setSnapshot(this.objectMapper.writeValueAsString(oldRule));
        } catch (JsonProcessingException e) {
            throw new BaseException(BaseConstants.ERR_UNKOWN);
        }
        boolean result = this.mixSectionMapper.update(rule)==1;
        return result;
    }
    @CacheEvict(value = "rule",cacheResolver = "ruleCacheResolver",allEntries = true)
    @Override
    public boolean del(String uid,List<String>ids) {
        boolean result = this.mixSectionMapper.del(uid,ids)>0;
        return result;
    }
}
